const express = require('express');
const upload = require('../module/file/upload')
const multer = require("multer");
const user = require("../module/user/user");
const router = express.Router();

// 上传视频
router.post('/uploadVideo',multer({ dest: 'uploads/' }).single('file'),async (req, res) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  if (loginStatus.loginStatus) {
    const data = await upload.upload(req)
    if (data.statusCode === 200) {
      const saveData = await upload.uploadVideo({
        ...loginStatus.loginRows,
        ...data
      })
      res.json(saveData)
    }else {
      res.json({
        code: 500,
        msg: '上传失败',
        data: null
      })
    }
  }else {
    res.json(loginStatus.msg)
  }
})

// 上传图片
router.post('/uploadImg',multer({ dest: 'uploads/' }).single('file'),async (req, res) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  if (loginStatus.loginStatus) {
    const data = await upload.upload(req)
    if (data.statusCode === 200) {
      const saveData = await upload.uploadImg({
        ...loginStatus.loginRows,
        ...data
      })
      res.json(saveData)
    }else {
      res.json({
        code: 500,
        msg: '上传失败',
        data: null
      })
    }
  }else {
    res.json(loginStatus.msg)
  }
})

// 上传实名认证图片
router.post('/uploadIdCard',multer({ dest: 'uploads/' }).single('file'),async (req, res) => {
  const loginStatus = await global.tokenFun.checkToken(req)
  if (loginStatus.loginStatus) {
    const data = await upload.upload(req)
    if (data.statusCode === 200) {
      const saveData = await upload.saveIdCard({
        ...loginStatus.loginRows,
        ...data
      })
      res.json(saveData)
    }else {
      res.json({
        code: 500,
        msg: '上传失败',
        data: null
      })
    }
  }else {
    res.json(loginStatus.msg)
  }
})

router.get('/getService',async (req, res) => {
  const data = await upload.getService(req.body)
  res.json(data)
})

module.exports = router;
